/*
 * Created: 2020-03-08 14:38:24
 * Author : Mockingbird
 * Email : 1768385508@qq.com
 * -----
 * Description: 整合所有路由
 *
 *
 */

import { GlobVar } from '../global';
import { Context } from 'koa';
import convert from 'koa-convert';
import bodyParser from 'koa-bodyparser';
import Router from 'koa-router';
import proxy from '../middlewares/proxy';
import { 
  handleLogin,
  handleLogout,
} from './../controllers/session';

import { renderLogin } from './../controllers/views'

const router = new Router();
import { getFileVersion, getServerConfig } from '../libs/utils';

import { EcosProxy } from '../proxy';

const { client: clientConfig, server: serverConfig } = getServerConfig();

const parseBody = convert(
  bodyParser({
    formLimit: '200kb',
    jsonLimit: '200kb',
  })
)

router
  // ecos api
  .use(proxy('/ecos/(.*)', EcosProxy))
  // session
  //.get('/validate_login', validateLogin)
  // session
  .post('/login', parseBody, handleLogin)
  .get('/login', renderLogin)
  // .post('/login/confirm', parseBody, handleLoginConfirm)
  // .get('/login/confirm', renderLoginConfirm)
  .get('/logout', handleLogout)
  // page entry
  .all('*', async (ctx: Context) => {
    await ctx.render('index', {
      t: ctx.t.bind(ctx),
      isDev: GlobVar.MODE_DEV,
      version: getFileVersion,
      title: clientConfig.title,
      config: JSON.stringify(clientConfig),
    });
  });

export { router };
